×
☰ See All Chapters

Handling Radio Button in Selenium Webdriver

The “click()” method is used to click on Buttons and Links (and other web elements). WebDriver supports Radio Button and Radio Group controls using the “WebElement” class. You can select and deselect the radio buttons using the “click()” method of the “WebElement” class and check whether a radio button is selected or deselected using the “isSelected()” method. You can also get all the radio buttons from a Radio Group in a list using the “findElements()” method along with the Radio Group identifier.

The below example explains how to automate drop down select field.

import org.openqa.selenium.By;

import org.openqa.selenium.Keys;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.support.ui.Select;

import org.junit.Assert;

 

public class Example {

 

        public static void main(String[] args) {

 

                // configure chromedriver

                System.setProperty("webdriver.chrome.driver", "F:\\My_Programs\\Selenium\\ChromeDriver\\chromedriver.exe");

 

                WebDriver driver = new ChromeDriver();

               

                // Launch website

                driver.get("https://localhost:8090/tools4testing/webfiles/contents/selenium/testpages/registration-form-testpage.html");

               

                //Select radio

                driver.findElement(By.id("mgender")).click();

                //Check is radio is selected

                Assert.assertTrue(driver.findElement(By.id("mgender")).isSelected());

               

                System.out.println("-------------------------------DONE----------------------------------");

                //wait some time before closing

                try {

                        Thread.sleep(7000);

                } catch (InterruptedException ie) {

                }

               

                //close the driver

                driver.quit();

        }

}

You can write the script and test these using our Test Page

selenium-webdriver-radio-button-0
 

All Chapters
Author